﻿#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion

using System;
using UnityEngine;

namespace Neatly.UI
{
    [DisallowMultipleComponent]
    [RequireComponent(typeof(SpriteRenderer))]
    [ExecuteAlways]
    public class NSprite : MonoBehaviour, ImageInterface
    {
        private SpriteRenderer m_Renderer;
        private SpriteRenderer Renderer
        {
            get
            {
                if (!m_Renderer)
                {
                    m_Renderer = gameObject.GetComponent<SpriteRenderer>();
                }
                return m_Renderer;
            }
        }
        [SerializeField]
        protected Sprite m_Sprite;
        [SerializeField]
        private string m_SpriteName = string.Empty;
        [SerializeField]
        private Material m_Material;
        public Sprite sprite
        {
            get { return m_Sprite; }
            set { m_Sprite = value; }
        }
        public string SpriteName
        {
            get { return m_SpriteName; }
            set { m_SpriteName = value; }
        }
        public Material material
        {
            get { return m_Material; }
            set { m_Material = value; }
        }

        void Awake()
        {
            if (NeatlyUI.AddAtlasRefAction != null)
            {
                NeatlyUI.AddAtlasRefAction(this);
            }
        }

        void OnDestroy()
        {
            if (!string.IsNullOrEmpty(m_SpriteName) && NeatlyUI.LoseAtlasRefAction != null)
            {
                NeatlyUI.LoseAtlasRefAction(this);
            }
        }

        public string GetSpriteName()
        {
            return SpriteName;
        }

        public void SetSprite(string spriteName, Sprite value, Material mat = null)
        {
            if (!SetPropertyUtility.SetClass(ref m_Sprite, value)) return;
            m_SpriteName = spriteName;
            SetPropertyUtility.SetClass(ref m_Material, mat);
            Renderer.material = m_Material;
            Renderer.sprite = m_Sprite;
        }

        public bool IsBlur()
        {
            return false;
        }

        public void SetSpriteName(string spriteName)
        {
            NeatlyUI.SetSpriteAction(this, spriteName);
        }

        public void SetColor(Color c)
        {
            Renderer.color = c;
        }

        public void SetColor(float r, float g, float b, float a = 1)
        {
            Renderer.color = new Color(r, g, b, a);
        }

        public void SetColor32(byte r, byte g, byte b, byte a = 255)
        {
            Renderer.color = new Color32(r, g, b, a);
        }

        public void SetColorAlpha(float val)
        {
            Color c = Renderer.color;
            c.a = val;
            Renderer.color = c;
        }

#if UNITY_EDITOR
        [Obsolete]
        public void RefreshSprite()
        {
            if (!string.IsNullOrEmpty(m_SpriteName))
            {
                m_Sprite = UIUtility.FindImageSprite(m_SpriteName, false, out m_Material);
                Renderer.material = m_Material;
                Renderer.sprite = m_Sprite;
            }
        }
#endif
    }
}